package com.cyou.nad.tag.table.pagebar;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.cyou.nad.dao.Pagination;

public class PageBar173App  extends AbstractPageBar{

	private static  PageBar173App bar = new PageBar173App();
	public static PageBar173App getInstance(){
		return bar;
	}
	@Override
	public String getHTML(HttpServletRequest req, Pagination page, String formAction) {
		Object obj = new Object();
		String formId = obj.hashCode()+"";
		String urlPageNo = req.getParameter(REQUEST_PARAMETER_PAGE);
		int currentPage = StringUtils.isBlank(urlPageNo) ? 1 : new Integer(urlPageNo).intValue();
		int totalPages = page.getTotalPageCount();
		int totalRows = page.getTotalCount();

		StringBuffer form = builderSearchForm(req, formId, formAction, totalRows, page.getPageSize());
		String navigation = getNav(formId, currentPage, totalPages, totalRows,page.getPageSize());
		return form.append(navigation).toString();
	}

	private String getNav(String formId, int currentPage, int totalPages, int totalRows, int pageSize) {
		
//		System.out.println(currentPage+"||"+totalPages+"||"+totalRows);
		// 显示分页链接的个数
		int numDisplayEntries = 5;
		// 开头结尾的链接数
		int numEdgeEntries = 1;
		// 链接
		String prevText = "上一页";
		String nextText = "下一页";
		String ellipsisText = "...";
	
		String pagingHtml = "";
		int[] interval = getInterval(currentPage, numDisplayEntries, totalPages);
//		System.out.println(Arrays.toString(interval));
		// 产生"上一页" 链接
		if (currentPage >1 ) {
			pagingHtml += createPageLink(true, false, prevText, formId, (currentPage-1), pageSize);
		}

		// 产生起始点
		if (interval[0] > 1 && numEdgeEntries > 0) {

			int end = Math.min(numEdgeEntries, interval[0]);
			for (int i = 0; i < end; i++) {
				pagingHtml += createPageLink(true, currentPage==1, (i + 1) + "", formId, i + 1, pageSize);
			}

			// 绘制省略号
			if (interval[0]>2 ) {
				pagingHtml += createEllipsisSpan(ellipsisText);
			}

		}
		// 产生内部的些链接
		for (int i = interval[0]; i <= interval[1]; i++) {
			
			boolean current = (i  == currentPage);
			boolean hasLink = !current;
			pagingHtml += createPageLink(hasLink, current, i  + "", formId, i, pageSize);
		}

		// 产生结束点
		if (interval[1] < totalPages && numEdgeEntries > 0) {
			// 绘制省略号
			if (interval[1] <totalPages - numEdgeEntries) {
				pagingHtml += createEllipsisSpan(ellipsisText);
			}

			pagingHtml += createPageLink(true, false, totalPages + "", formId, totalPages, pageSize);
		}

		// 产生 "下一页" 链接
		if (currentPage < totalPages ) {
			pagingHtml += createPageLink(true, currentPage==totalPages, nextText, formId, currentPage+1, pageSize);
		}

		if (totalPages != 0) {
			return pagingHtml + getJsFunction(formId);
		} else {
			return "没有任何题目!";
		}
	}
	
	private String createEllipsisSpan(String ellipsisText){
		if(StringUtils.isNotBlank(ellipsisText)){
			return "<li><a href='javascript:void(0)'><span>"+ellipsisText+"</span></a></li>";
		}else{
			return "";
		}
	}
	
	private int[] getInterval(int currentPage,int numDisplayEntries,int totalPages){
		int half =  (int) Math.ceil(((double)numDisplayEntries)/(double)2);
		int start = currentPage-half;
		if(start<=0)
			start=1;
		int end = numDisplayEntries+start;
		if(end>totalPages){
			end=totalPages;
		}
		if(totalPages>6){
			if(start==1&&end==6){
				end=5;
			}
			if(end==totalPages){
				start=end-numDisplayEntries+1;
			}
		}
		return new int[]{start,end};
	}
	
	private String createPageLink(boolean hasLink,boolean currentPage,String text,String formId,int pageNo,int pageSize ){
		if(currentPage){
			return "<li><a  style='cursor: pointer;' class='current'><span>"+text+"</span></a></li>";
		}else if(!hasLink){
			return "<li><a tyle='cursor: pointer;' class='current'><span>"+text+"</span></a></li>";
		}else{
			return "<li><a tyle='cursor: pointer;' onclick=\"gotoPage"+formId+"('" + formId + "'," + pageNo + ","+pageSize+");\"><span>"+text+"</span></a></li>";
		}
	}
	
	private String getJsFunction (String formId){
		String js ="<script type = 'text/javaScript'>\r\n";
		js+="function gotoPage"+formId+"(formId,pageNo,pageSize){" +
			"if(pageSize){ " +
			"	var form = document.getElementById('"+formId+"');" +
			"	form."+REQUEST_PARAMETER_PAGE_SIZE+".value = pageSize;" +
			"}" +
			"var pageId = '"+ REQUEST_PARAMETER_PAGE+ formId+ "';" +
			"var pageInput = document.getElementById(pageId);	" +
			"pageInput.value=pageNo;document.getElementById(formId).submit();}";
		js+="</script>";
		return js;
	}
	
	
	public static void main(String[] args) {
		PageBar173App app = PageBar173App.getInstance();
		System.out.println(Arrays.toString(app.getInterval(6, 5, 22)));
		System.out.println(Arrays.toString(app.getInterval(1, 5, 22)));
	}
}
